<template>
	<view class="container">
		<!-- 优惠券类型切换 -->
		<view class="coupon-tabs">
			<view 
				class="tab-item" 
				:class="{'active': currentTab === 'available'}"
				@click="switchTab('available')"
			>
				<text>可使用</text>
			</view>
			<view 
				class="tab-item" 
				:class="{'active': currentTab === 'used'}"
				@click="switchTab('used')"
			>
				<text>已使用</text>
			</view>
			<view 
				class="tab-item" 
				:class="{'active': currentTab === 'expired'}"
				@click="switchTab('expired')"
			>
				<text>已过期</text>
			</view>
		</view>
		
		<!-- 优惠券列表 -->
		<scroll-view 
			scroll-y 
			class="coupon-scroll"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="coupon-list">
				<view 
					class="coupon-item" 
					v-for="(item, index) in couponList" 
					:key="index"
					:class="{'used': currentTab !== 'available', 'expired': currentTab === 'expired'}"
				>
					<view class="coupon-left">
						<view class="amount">
							<text class="currency">¥</text>
							<text class="value">{{item.amount}}</text>
						</view>
						<text class="condition">满{{item.condition}}元可用</text>
					</view>
					<view class="coupon-right">
						<view class="info">
							<text class="name">{{item.name}}</text>
							<text class="time">有效期至：{{item.expireTime}}</text>
						</view>
						<view class="action">
							<text class="status" v-if="currentTab !== 'available'">{{currentTab === 'used' ? '已使用' : '已过期'}}</text>
							<button class="use-btn" v-else @click="useCoupon(item)">立即使用</button>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="couponList.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无优惠券</text>
				<view class="get-coupon" @click="getCoupon">
					<text>领取优惠券</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

// 当前选中的标签
const currentTab = ref('available')

// 是否还有更多数据
const hasMore = ref(true)

// 是否正在刷新
const isRefreshing = ref(false)

// 优惠券列表
const couponList = ref([
	{
		id: 1,
		name: '新人专享券',
		amount: '10',
		condition: '100',
		expireTime: '2024-12-31'
	},
	{
		id: 2,
		name: '满减优惠券',
		amount: '50',
		condition: '300',
		expireTime: '2024-12-31'
	},
	{
		id: 3,
		name: '节日特惠券',
		amount: '100',
		condition: '500',
		expireTime: '2024-12-31'
	}
])

// 切换标签
const switchTab = (tab) => {
	currentTab.value = tab
	// 重置列表并重新加载
	couponList.value = []
	hasMore.value = true
	loadMore()
}

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多优惠券的接口调用
	setTimeout(() => {
		hasMore.value = false
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	// 重置列表并重新加载
	couponList.value = []
	hasMore.value = true
	
	setTimeout(() => {
		isRefreshing.value = false
		loadMore()
	}, 1000)
}

// 使用优惠券
const useCoupon = (coupon) => {
	uni.navigateTo({
		url: '/pages/index/index'
	})
}

// 领取优惠券
const getCoupon = () => {
	uni.navigateTo({
		url: '/pages/index/index'
	})
}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
}

/* 优惠券类型切换样式 */
.coupon-tabs {
	display: flex;
	background-color: #FFFFFF;
	padding: 20rpx 0;
	margin-bottom: 20rpx;
}

.tab-item {
	flex: 1;
	text-align: center;
	font-size: 28rpx;
	color: #666;
	position: relative;
}

.tab-item.active {
	color: #D02B29;
	font-weight: bold;
}

.tab-item.active::after {
	content: '';
	position: absolute;
	left: 50%;
	bottom: -20rpx;
	transform: translateX(-50%);
	width: 40rpx;
	height: 4rpx;
	background-color: #D02B29;
	border-radius: 2rpx;
}

/* 优惠券列表样式 */
.coupon-scroll {
	height: calc(100vh - 100rpx);
}

.coupon-list {
	padding: 20rpx;
}

.coupon-item {
	display: flex;
	background-color: #FFFFFF;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
	overflow: hidden;
	position: relative;
}

.coupon-item.used::after,
.coupon-item.expired::after {
	content: '';
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
}

.coupon-left {
	width: 240rpx;
	background-color: #D02B29;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.amount {
	display: flex;
	align-items: baseline;
}

.currency {
	font-size: 32rpx;
	color: #FFFFFF;
}

.value {
	font-size: 60rpx;
	font-weight: bold;
	color: #FFFFFF;
}

.condition {
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.8);
	margin-top: 10rpx;
}

.coupon-right {
	flex: 1;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.info {
	flex: 1;
}

.name {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 20rpx;
	display: block;
}

.time {
	font-size: 24rpx;
	color: #999;
}

.action {
	display: flex;
	justify-content: flex-end;
	align-items: center;
}

.status {
	font-size: 28rpx;
	color: #999;
}

.use-btn {
	width: 160rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	background-color: #D02B29;
	color: #FFFFFF;
	font-size: 24rpx;
	border-radius: 30rpx;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 24rpx;
	color: #999;
}

/* 空状态样式 */
.empty-state {
	padding: 100rpx 0;
	text-align: center;
}

.empty-img {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
	margin-bottom: 40rpx;
	display: block;
}

.get-coupon {
	display: inline-block;
	padding: 20rpx 60rpx;
	background-color: #D02B29;
	color: #FFFFFF;
	font-size: 28rpx;
	border-radius: 40rpx;
}
</style> 